﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Icms.Core.Model;
using Icms.Core.Repository;
using Omu.Awesome.Mvc;

namespace Icms.WebUI.Controllers
{
    public class UsersLookupController : LookupController
    {
        private readonly IRepo<User> repo;

        public UsersLookupController(IRepo<User> repo)
        {
            this.repo = repo;
        }

        public override ActionResult SearchForm()
        {
            return Content("");
        }

        [HttpPost]
        public ActionResult Search(IEnumerable<Guid> selected)
        {
            var result = repo.GetAll().Where(o => selected == null || !selected.Contains(o.Id)).ToList();
            return View(@"Awesome\LookupList", result);
        }

        public ActionResult Selected(IEnumerable<Guid> selected)
        {
            return View(@"Awesome\LookupList", repo.GetAll().Where(o => selected != null && selected.Contains(o.Id)));
        }

        public ActionResult GetMultiple(IEnumerable<Guid> selected)
        {
            return Json(repo.GetAll().Where(o => selected.Contains(o.Id)).Select(v => new { Text = v.FirstName + " " + v.LastName }));
        }
    }
}